package io.kiki.sba.registry.server.clientInterface.resource;

import io.kiki.sba.registry.common.model.ClientToClientInterfaceServerNodeId;
import io.kiki.sba.registry.common.model.Response;
import io.kiki.sba.registry.common.model.sessionserver.CancelAddressRequest;
import io.kiki.sba.registry.server.clientInterface.registry.Registry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("api/clients")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ClientsOpenResource {

    @Autowired
    private Registry sessionRegistry;


    @POST
    @Path("/off")
    public Response clientOff(CancelAddressRequest request) {

        if (null == request) {
            return Response.buildFailedResponse("Request can not be null.");
        }

        if (CollectionUtils.isEmpty(request.getClientToClientInterfaceServerNodeIds())) {
            return Response.buildFailedResponse("ConnectIds can not be null.");
        }

        final List<ClientToClientInterfaceServerNodeId> clientToClientInterfaceServerNodeIds = request.getClientToClientInterfaceServerNodeIds();
        sessionRegistry.clean(clientToClientInterfaceServerNodeIds);
        return Response.buildSuccessResponse();
    }
}
